package com.power.project.dao.impl;

import com.power.project.dao.Ww_tagDaoCustom;
import com.power.project.model.Ww_tag;
import com.power.project.model.pageInfo.Ww_tagPageInfo;
import com.power.project.model.vo.TagVO;
import com.power.project.utils.PageHelperUtil;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.List;

import static com.power.project.dao.impl.Ww_userDaoImpl.BLANK_STRING;

/**
 * 标签 持久层实现(hibernate)
 */

@Repository
public class Ww_tagDaoImpl implements Ww_tagDaoCustom<Ww_tag, Serializable> {
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Page<TagVO> findAll(Ww_tagPageInfo pageInfo, Pageable pageable) {
        StringBuilder dataSql = new StringBuilder(
                "select wt.tagid as \"tagId\", wt.name, group_concat(distinct tmp.name) as users\n" +
                        "from ww_tag wt\n" +
                        "         left join ww_usertag wut on wt.tagid = wut.tagid and wut.tenantid = :tenantId\n" +
                        "         left join (select wu.*\n" +
                        "                    from ww_user wu\n" +
                        "                             left join ha_user hu\n" +
                        "                                       on wu.userid = hu.userid and hu.deleted = 0 and wu.tenantid = :tenantId) tmp\n" +
                        "                   on tmp.userid = wut.wwuserid").append(BLANK_STRING);
        StringBuilder countSql = new StringBuilder(
                "select 1 " +
                        "from ww_tag wt\n" +
                        "         left join ww_usertag wut on wt.tagid = wut.tagid and wut.tenantid = :tenantId\n" +
                        "         left join (select wu.*\n" +
                        "                    from ww_user wu\n" +
                        "                             left join ha_user hu\n" +
                        "                                       on wu.userid = hu.userid and hu.deleted = 0 and wu.tenantid = :tenantId) tmp\n" +
                        "                   on tmp.userid = wut.wwuserid").append(BLANK_STRING);

        StringBuilder whereSql = new StringBuilder("where wt.tenantid = :tenantId").append(BLANK_STRING);

        StringBuilder orderSql = new StringBuilder("");

        StringBuilder groupSql = new StringBuilder("group by wt.tagid").append(BLANK_STRING);

        if (StringUtils.isNotEmpty(pageInfo.getF_name())) {
            whereSql.append("and wt.name like concat('%', :name, '%')").append(BLANK_STRING);
        }

        dataSql.append(whereSql).append(groupSql);
        countSql.append(whereSql).append(groupSql);
        String countSql1 = String.format("select count(1) from (%s) tmp_count", countSql);

        //创建本地sql查询实例
        Query dataQuery = entityManager.createNativeQuery(dataSql.toString());
        Query countQuery = entityManager.createNativeQuery(countSql1);

        dataQuery.setParameter("tenantId", pageInfo.getF_tenantid());
        countQuery.setParameter("tenantId", pageInfo.getF_tenantid());

        //设置参数
        if (StringUtils.isNotEmpty(pageInfo.getF_name())) {
            dataQuery.setParameter("name", pageInfo.getF_name());
            countQuery.setParameter("name", pageInfo.getF_name());
        }

        return PageHelperUtil.getPage(pageable, dataQuery, countQuery, TagVO.class);
    }

    @Override
    public List<TagVO> findAllTags(String tenantId) {
        String dataSql = "select w.tagid as \"tagId\",name from Ww_tag w where w.tenantid = :tenantId";
        Query dataQuery = entityManager.createNativeQuery(dataSql);

        dataQuery.setParameter("tenantId", tenantId);

        return dataQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(TagVO.class)).getResultList();
    }
}